package com.atzxh.java;

/**
 * @author 小红同学
 * @creat 2021-11-09-10:19
 * @description    解决继承 Thread 类的线程安全问题
 */
class Window3 extends Thread{

    private static int ticket = 100;
    private static Object obj = new Object();

    public void run() {
        while (true){
            try {
                sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //synchronized(obj){
            synchronized(Window3.class){   //类本身也是对象,但是不能用 this
                if(ticket > 0){
                    System.out.println(getName()+":"+ticket);
                    ticket--;
                }else{
                    break;
                }
            }
        }
    }
}


public class WindowTest2 {
    public static void main(String[] args) {
        Window3 w1 = new Window3();
        Window3 w2 = new Window3();
        Window3 w3 = new Window3();

        w1.setName("窗口1");
        w2.setName("窗口2");
        w3.setName("窗口3");

        w1.start();
        w2.start();
        w3.start();
    }
}
